package com.virgo.common.utils;

import lombok.experimental.UtilityClass;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

@UtilityClass
public class ITimeUtils {

	/**
	 * 获取当前UTC时间戳
	 * @return int 时间戳
	 */
	public Long gmTime(){
		return Instant.now().getEpochSecond();
	}

	/**
	 * 格式化时间戳为时间字符串
	 * @param format 时间格式
	 * @param timeStamp 时间戳
	 * @return 时间格式字符串
	 */
	public String localDate(String format,Long timeStamp) {
		if (format.isBlank() || timeStamp.compareTo(0L) < 1) {
			return "";
		}
		DateTimeFormatter ftf = DateTimeFormatter.ofPattern(format);
		return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(timeStamp), ZoneId.systemDefault()));
	}

	/**
	 * 格式化时间戳为时间字符串
	 * @param format 时间格式
	 * @return 时间格式字符串
	 */
	public String localDate(String format) {
		Long nowTime = gmTime();
		return localDate(format,nowTime);
	}

	/**
	 * 格式化时间戳为时间字符串
	 * @param timeStamp 时间戳
	 * @return 时间格式字符串
	 */
	public String localDate(Long timeStamp) {
		return localDate("yyyy-MM-dd HH:mm",timeStamp);
	}

	/**
	 * 格式化当前时间为时间字符串
	 * @return 时间格式字符串
	 */
	public String localDate() {
		Long nowTime = gmTime();
		return localDate("yyyy-MM-dd HH:mm:ss",nowTime);
	}

	/**
	 * 字符串时间转时间戳 这里需要指定 yyyy-MM-dd HH:mm:ss
	 * @param format 时间格式字符串
	 * @return 基于UTC的时间戳
	 */
	public Long gmStr2time(String format){
		DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime parse = LocalDateTime.parse(format,ftf);
		return parse.atZone(ZoneId.systemDefault()).toEpochSecond();
	}

	/**
	 * 获取今天00:00:00的时间
	 * @return long
	 */
	public long getTodayStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		long dateTime = todayStart.getTime().getTime();
		return dateTime/1000L;
	}

	/**
	 * 获取今天23:59:59的时间
	 * @return long
	 */
	public long getTodayEndTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 23);
		todayStart.set(Calendar.MINUTE, 59);
		todayStart.set(Calendar.SECOND, 59);
		todayStart.set(Calendar.MILLISECOND, 59);
		long dateTime = todayStart.getTime().getTime();
		dateTime /= 1000L;
		return dateTime/1000L;
	}

	/**
	 * 获取本月第一天00:00:00的时间
	 * @return long
	 */
	public long getThisMonthStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.DAY_OF_MONTH,1);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		long dateTime = todayStart.getTime().getTime();
		dateTime /= 1000L;
		return dateTime/1000L;
	}

	/**
	 * 获取本月第一天00:00:00的时间
	 * @return long
	 */
	public long getLastMonthStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.add(Calendar.MONTH,-1);
		todayStart.set(Calendar.DAY_OF_MONTH,1);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		long dateTime = todayStart.getTime().getTime();
		dateTime /= 1000L;
		return dateTime/1000L;
	}
}
